package admin.forms;

import helpers.LanguageHelper;
import helpers.StringHelper;
import helpers.ValidateHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import admin.models.ArticleModel;

import com.sun.org.apache.xpath.internal.compiler.Keywords;

import entities.Article;

public class ArticleForm {
	public static TreeMap checkValid(HttpServletRequest request, Article article) {
		TreeMap form = new TreeMap();
		TreeMap checkValid = new TreeMap();
		
		String appId = (String) request.getAttribute("appId");
		String title = (String) request.getParameter("title");
		String content = (String) request.getParameter("content");
		String category = (String) request.getParameter("category");
		String authorName = (String) request.getParameter("authorName");
		String email = (String) request.getParameter("email");
		String website = (String) request.getParameter("website");
		String keywords = (String) request.getParameter("keywords");
		String status = (String) request.getParameter("status");
		
		
		//Set old alias
		article.setOldAlias(article.getAlias());
		
		if (title == null || title.isEmpty()) {
			checkValid.put("title", "Title is not empty.");
			article.setTitle("");
		} else {
			article.setTitle(title);
		}
		
		if (title != null) {
			String alias = StringHelper.replace(title);
			ArticleModel articleModel = ArticleModel.getInstance();
			if (!alias.isEmpty() && alias.equalsIgnoreCase(article.getAlias())) {
				article.setAlias(alias);
			} else if (alias.isEmpty() || articleModel.checkAliasExists(appId, alias, article.getLanguage())) {
				checkValid.put("title", "Article with this title already exists.");
				article.setAlias(alias);
			} else {
				article.setAlias(alias);
			}
		}
		
		if (content == null || content.isEmpty()) {
			checkValid.put("content", "Content is not empty.");
			article.setContent("");
		} else {
			Document doc = Jsoup.parse(content);
			Elements elms = doc.select("a");
			if (elms.size() > 5) {
				checkValid.put("content", "Maximum link: 5");
			}
			article.setContent(content);
		}
		
		if (category == null || category.isEmpty()) {
			checkValid.put("category", "Please choose a category");
			article.setCategory("");
		} else {
			article.setCategory(category);
		}
		
		if (keywords != null && !keywords.isEmpty()) {
			String[] k = keywords.split(",");
			List<String> keys = new ArrayList<String>();
			for (int i = 0; i < k.length; i++) {
				keys.add(k[i].trim());
			}
			
			if (k.length > 5) {
				checkValid.put("keywords", "Maximum: 5 keywords");
			}
			
			
			article.setKeywords(keys);
			article.setTags(keywords);
		}
		
		if (status != null)
			article.setStatus(Integer.parseInt(status));
		
		if (authorName == null || authorName.isEmpty()) {
			checkValid.put("authorName", "Please input author name.");
			article.setAuthorName("");
		} else {
			article.setAuthorName(authorName);
		}
		
		if (email == null || email.isEmpty()) {
			checkValid.put("email", "Please input author email.");
			article.setEmail("");
		} else if (!ValidateHelper.isEmail(email)) {
			checkValid.put("email", "Author email is not valid");
			article.setEmail(email);
		} else {	
			article.setEmail(email);
		}
		
		if (website != null) {
			article.setWebsite(website);
		}
		
		
		form.put("checkValid", checkValid);
		form.put("article", article);
		
		return form;
	}
}
